/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @CountdownView.java  2014-9-15 下午8:00:05 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.view;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseApplication;
import com.android.tiango.baba.util.TimeToUtil;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

public class CountdownView extends LinearLayout {
	// private View view;
	public TextView tv_3, tv_4, tv_5, tv_6;// tv_1, tv_2,
	public TextView tv_3_3, tv_4_4, tv_5_5, tv_6_6;// tv_1_1, tv_2_2,
	// public TextView tv_d1, tv_d2, tv_d1_1, tv_d2_1;
	public AppTimeCount mAppTimeCount;
	private long time;
	private Handler mHandler;

	private String tx3, tx4, tx5, tx6, tx7;// tx0, tx1, tx2,

	// private TextView tv_point2, tv_point1;

	public CountdownView(Context context) {
		super(context);
		this.initView(context);
	}

	public CountdownView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		this.initView(context);
	}

	public CountdownView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.initView(context);
	}

	private void initView(Context context) {
		String infService = Context.LAYOUT_INFLATER_SERVICE;
		LayoutInflater li = (LayoutInflater) getContext().getSystemService(infService);
		li.inflate(R.layout.layout_view_timereverse, this, true);

		// view = LayoutInflater.from(context).inflate(R.layout.layout_view_timereverse, null);
		// tv_1 = (TextView) findViewById(R.id.tv_1);
		// tv_2 = (TextView) findViewById(R.id.tv_2);
		tv_3 = (TextView) findViewById(R.id.tv_3);
		tv_4 = (TextView) findViewById(R.id.tv_4);
		tv_5 = (TextView) findViewById(R.id.tv_5);
		tv_6 = (TextView) findViewById(R.id.tv_6);
		// tv_1_1 = (TextView) findViewById(R.id.tv_1_1);
		// tv_2_2 = (TextView) findViewById(R.id.tv_2_2);
		tv_3_3 = (TextView) findViewById(R.id.tv_3_3);
		tv_4_4 = (TextView) findViewById(R.id.tv_4_4);
		tv_5_5 = (TextView) findViewById(R.id.tv_5_5);
		tv_6_6 = (TextView) findViewById(R.id.tv_6_6);
		// tv_d1 = (TextView) findViewById(R.id.tv_d1);
		// tv_d2 = (TextView) findViewById(R.id.tv_d2);
		// tv_d1_1 = (TextView) findViewById(R.id.tv_d1_1);
		// tv_d2_1 = (TextView) findViewById(R.id.tv_d2_1);
	}

	public void setTime(Handler handler, long time) {
		this.mHandler = handler;
		this.time = time;
		if (mAppTimeCount == null) {
			mAppTimeCount = new AppTimeCount(time - BaBaseApplication.getmServerTime());
			mAppTimeCount.start();
		} else {
			mAppTimeCount.cancel();
			mAppTimeCount = null;
			mAppTimeCount = new AppTimeCount(time - BaBaseApplication.getmServerTime());
			mAppTimeCount.start();
		}
	}

	private void showAnnim1(TextView textView, TextView textView2) {
		ViewHelper.setPivotX(textView, textView.getWidth() / 2f);
		ViewHelper.setPivotY(textView, textView.getHeight() / 2f);
		ObjectAnimator.ofFloat(textView, "rotationX", 180, 0).setDuration(400).start();
		ObjectAnimator.ofFloat(textView2, "rotationX", 0, 180).setDuration(400).start();
		ObjectAnimator.ofFloat(textView, "alpha", 0, 1).setDuration(400).start();
		ObjectAnimator.ofFloat(textView2, "alpha", 1, 0).setDuration(400).start();
		textView.setVisibility(View.VISIBLE);
		textView2.setVisibility(View.INVISIBLE);
	}

	private void showAnnim2(TextView textView, TextView textView2) {
		ViewHelper.setPivotX(textView, textView.getWidth() / 2f);
		ViewHelper.setPivotY(textView, textView.getHeight() / 2f);
		ObjectAnimator.ofFloat(textView, "rotationX", 0, 180).setDuration(400).start();
		ObjectAnimator.ofFloat(textView2, "rotationX", 180, 0).setDuration(400).start();
		ObjectAnimator.ofFloat(textView, "alpha", 1, 0).setDuration(400).start();
		ObjectAnimator.ofFloat(textView2, "alpha", 0, 1).setDuration(400).start();
		textView.setVisibility(View.INVISIBLE);
		textView2.setVisibility(View.VISIBLE);
	}

	private void showAnnim(TextView textView, TextView textView2) {

		if (textView.getVisibility() == View.INVISIBLE) {
			showAnnim1(textView, textView2);
		} else {
			showAnnim2(textView, textView2);
		}
	}

	public class AppTimeCount extends CountDownTimer {

		public AppTimeCount(long millisInFuture) {
			super(millisInFuture, 1000);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			long[] left = TimeToUtil.getCountDown1(millisUntilFinished);
			showTime(left);

		}

		@Override
		public void onFinish() {
			// tv_1.setBackgroundResource(R.drawable.time_0);
			// tv_2.setBackgroundResource(R.drawable.time_0);
			tv_3.setBackgroundResource(R.drawable.time_0);
			tv_4.setBackgroundResource(R.drawable.time_0);
			tv_5.setBackgroundResource(R.drawable.time_0);
			tv_6.setBackgroundResource(R.drawable.time_0);
			// tv_1_1.setBackgroundResource(R.drawable.time_0);
			// tv_2_2.setBackgroundResource(R.drawable.time_0);
			tv_3_3.setBackgroundResource(R.drawable.time_0);
			tv_4_4.setBackgroundResource(R.drawable.time_0);
			tv_5_5.setBackgroundResource(R.drawable.time_0);
			tv_6_6.setBackgroundResource(R.drawable.time_0);
			// tv_d1.setBackgroundResource(R.drawable.time_0);
			// tv_d2.setBackgroundResource(R.drawable.time_0);
			// tv_d1_1.setBackgroundResource(R.drawable.time_0);
			// tv_d2_1.setBackgroundResource(R.drawable.time_0);
			if (mAppTimeCount != null) {
				mAppTimeCount.cancel();
				mAppTimeCount = null;
				if (null != mHandler) {
					mHandler.removeMessages(0);
					mHandler.sendMessageDelayed(mHandler.obtainMessage(0), 1000);
					mHandler = null;
				}
				cancel();
			}
		}

		private void showTime(long[] timeLeft) {
			// tx0 = String.valueOf(timeLeft[0]);
			// tx1 = String.valueOf(timeLeft[1]);
			// tx2 = String.valueOf(timeLeft[2]);
			tx3 = String.valueOf(timeLeft[3]);
			tx4 = String.valueOf(timeLeft[4]);
			tx5 = String.valueOf(timeLeft[5]);
			tx6 = String.valueOf(timeLeft[6]);
			tx7 = String.valueOf(timeLeft[7]);
			// if (!tv_d1.getText().toString().equals(tx0)) {
			// showAnnim(tv_d1, tv_d1_1);
			// }
			// if (!tv_d2.getText().toString().equals(tx1)) {
			// showAnnim(tv_d2, tv_d2_1);
			// }
			// if (!tv_1.getText().toString().equals(tx2)) {
			// showAnnim(tv_1, tv_1_1);
			// }
			// if (!tv_2.getText().toString().equals(tx3)) {
			// showAnnim(tv_2, tv_2_2);
			// }

			if (!tv_3.getTag().toString().equals(tx4)) {
				showAnnim(tv_3, tv_3_3);
			}
			if (!tv_4.getTag().toString().equals(tx5)) {
				showAnnim(tv_4, tv_4_4);
			}
			if (!tv_5.getTag().toString().equals(tx6)) {
				showAnnim(tv_5, tv_5_5);
			}
			if (!tv_6.getTag().toString().equals(tx7)) {
				showAnnim(tv_6, tv_6_6);
			}

			// tv_d1.setBackgroundResource(getResId(tx0));
			// tv_d2.setBackgroundResource(getResId(tx1));
			// tv_d1_1.setBackgroundResource(getResId(tx0));
			// tv_d2_1.setBackgroundResource(getResId(tx1));
			// tv_1.setBackgroundResource(getResId(tx2));
			// tv_2.setBackgroundResource(getResId(tx3));
			tv_3.setBackgroundResource(getResId(tx4));
			tv_3.setTag(tx4);
			tv_4.setBackgroundResource(getResId(tx5));
			tv_4.setTag(tx5);
			tv_5.setBackgroundResource(getResId(tx6));
			tv_5.setTag(tx6);
			tv_6.setBackgroundResource(getResId(tx7));
			tv_6.setTag(tx7);
			// tv_1_1.setBackgroundResource(getResId(tx2));
			// tv_2_2.setBackgroundResource(getResId(tx3));
			tv_3_3.setBackgroundResource(getResId(tx4));
			tv_3_3.setTag(tx4);
			tv_4_4.setBackgroundResource(getResId(tx5));
			tv_4_4.setTag(tx5);
			tv_5_5.setBackgroundResource(getResId(tx6));
			tv_5_5.setTag(tx6);
			tv_6_6.setBackgroundResource(getResId(tx7));
			tv_6_6.setTag(tx7);
			// requestLayout();
		}

	}

	private int getResId(String num) {
		int resId = 0;
		if (num.equals("0")) {
			resId = R.drawable.time_0;
		} else if (num.equals("1")) {
			resId = R.drawable.time_1;
		} else if (num.equals("2")) {
			resId = R.drawable.time_2;
		} else if (num.equals("3")) {
			resId = R.drawable.time_3;
		} else if (num.equals("4")) {
			resId = R.drawable.time_4;
		} else if (num.equals("5")) {
			resId = R.drawable.time_5;
		} else if (num.equals("6")) {
			resId = R.drawable.time_6;
		} else if (num.equals("7")) {
			resId = R.drawable.time_7;
		} else if (num.equals("8")) {
			resId = R.drawable.time_8;
		} else if (num.equals("9")) {
			resId = R.drawable.time_9;
		}
		return resId;
	}
}
